// "icon":{
// 	"key": "field",
// 	"href": "image/column.png",
// 	"width": 32,
// 	"height": 32
// },

// 显示样式
var  STYLE = {
	NODE: {
		"text": {
			"opacity": 1,
			"stroke": "#000",
			"strokeWidth": "0",
			"strokeDasharray": 0
		},
		"opacity": 1,
		"stroke": "#000",
		"strokeWidth": "0",
		"strokeDasharray": 0,
		"width": 32,
		"height": 32,
		"radius": 20
	},
	TABLE: {
		"text": {
			"opacity": 1,
			"stroke": "#000",
			"strokeWidth": "0",
			"strokeDasharray": 0
		},
		"icon":{
			"key": "table",
			"href": "image/table.png",
			"width": 32,
			"height": 32
		},
		"opacity": 1,
		"stroke": "#000",
		"strokeWidth": "0",
		"strokeDasharray": 0,
		"width": 32,
		"height": 32,
		"radius": 0,
		"fill": "#03A9F4"
	},
	TABLETEMP: {
		"text": {
			"opacity": 1,
			"stroke": "#000",
			"strokeWidth": "0",
			"strokeDasharray": 0
		},
		"icon":{
			"key": "tableTemp",
			"href": "image/tableTemp.png",
			"width": 32,
			"height": 32
		},
		"opacity": 1,
		"stroke": "#000",
		"strokeWidth": "0",
		"strokeDasharray": 0,
		"width": 32,
		"height": 32,
		"radius": 0,
		"fill": "#03A9F4"
	},
	FIELD: {
		"text": {
			"opacity": 1,
			"stroke": "#000",
			"strokeWidth": "0",
			"strokeDasharray": 0
		},
		"opacity": 1,
		"stroke": "#000",
		"strokeWidth": "0",
		"strokeDasharray": 0,
		"width": 32,
		"height": 32,
		"radius": 0,
		"fill": "#00E5FF"
	},
	PROCEDURE: {
		"text": {
			"opacity": 1,
			"stroke": "#000",
			"strokeWidth": "0",
			"strokeDasharray": 0
		},
		"icon":{
			"key": "database",
			"href": "image/database.png",
			"width": 32,
			"height": 32
		},
		"opacity": 1,
		"stroke": "#000",
		"strokeWidth": "0",
		"strokeDasharray": 0,
		"width": 32,
		"height": 32,
		"radius": 0,
		"fill": "#26A69A"
	},
	EVENT: {
		"text":{
			"opacity": 0,
			"stroke":"#000",
			"strokeDasharray":0
		},
        "opacity" : 0,
        "stroke":"#000",
        "strokeWidth":"2px",
        "strokeDasharray":0,
		"fill": "#424242"
	},
	INPUT: {
		"text":{
			"opacity": 0,
			"stroke":"#000",
			"strokeDasharray":0
		},
        "opacity" : 0,
        "stroke":"#03A9F4",
        "strokeWidth":"2px",
        "strokeDasharray":0,
        "fill": "#03A9F4"
	},
	OUTPUT: {
		"text":{
			"opacity": 0,
			"stroke":"#000",
			"strokeDasharray":0
		},
        "opacity" : 0,
        "stroke":"#FF3D00",
        "strokeWidth":"2px",
        "strokeDasharray":0,
        "fill": "#FF3D00"
	},
	VALUE_FROM: {
		"text":{
			"opacity": 0,
			"stroke":"#000",
			"strokeDasharray":0
		},
        "opacity" : 0,
        "stroke":"#FFAB00",
        "strokeWidth":"2px",
        "strokeDasharray":0,
        "fill": "#FFAB00"
	}
};
// sd 对应属性
var  MAPPING = {
	"Table": {
		style: STYLE.TABLE
	},
	"TableTemp": {
		style: STYLE.TABLETEMP
	},
	"Field": {
		style: STYLE.FIELD
	},
	"Procedure": {
		style: STYLE.PROCEDURE
	},
	"VALUE_FROM": {
		style: STYLE.VALUE_FROM
	},
	"INPUT": {
		style: STYLE.INPUT
	},
	"OUTPUT": {
		style: STYLE.OUTPUT
	},
	"NODE": {
		style: STYLE.NODE
	},
	"EVENT": {
		style: STYLE.EVENT
	}
};
// 关系箭头
const EVENT_ARROW = {
	"KEY":"arrow",
	"POSITIVE": "positive",
	"REVERSE":"reverse",
	"TWOWAY":"twoway"
};
// title
const TITLE_PROPS = {
	"Table": ["name"],
	"TableTemp": ["name"],
	"Field": ["name"],
	"Procedure": ["name"],
	"VALUE_FROM": ["procedure"],
	"INPUT":[],
	"OUTPUT":[]
};
// 节点、关系颜色
var NODE_COLORS = {},
	EVENT_COLORS = {},
	NODE_GROUP = {};
var DataUtil = {
	// 生成节点组信息
	generateNodeGroup: function(sdName){
		if(NODE_GROUP[sdName]){
			return NODE_GROUP[sdName];
		}
		let curGroup = Object.keys(NODE_GROUP).length + 1;
		NODE_GROUP[sdName] = curGroup;
		return curGroup;
	},
	// 生成节点颜色
	generateNodeFill: function(sdName){
		if(NODE_COLORS[sdName]){
			return NODE_COLORS[sdName];
		}
		let curColor = '#'+Math.floor(Math.random()*0xffffff).toString(16);
		NODE_COLORS[sdName] = curColor;
		return curColor;
	},
	// 克隆对象
	cloneObject: function(objectToBeCloned){
		// Basis.
	   if (!(objectToBeCloned instanceof Object)) {
		   return objectToBeCloned;
	   }
	   let objectClone = null;
	   // Filter out special objects.
	   let Constructor = objectToBeCloned.constructor;
	   switch (Constructor) {
		   // Implement other special objects here.
		   case RegExp:
			   objectClone = new Constructor(objectToBeCloned);
			   break;
		   case Date:
			   objectClone = new Constructor(objectToBeCloned.getTime());
			   break;
		   default:
			   objectClone = new Constructor();
	   }
	   // Clone each property.
	   for (const prop of Object.keys(objectToBeCloned)) {
		   objectClone[prop] = DataUtil.cloneObject(objectToBeCloned[prop]);
	   }
	   return objectClone;
	},
	// 转换数据
	transformData: function(queryResult){
		DataUtil.dataIdMap = {};
		let result = {
			"nodes":[],
			"links":[]
		};
		// uxdf 数据
		let uxdf_data = queryResult,
			uxdf_nodes = [],
			uxdf_events = [];
		// 节点
		if(uxdf_data && uxdf_data.node){
			uxdf_nodes = uxdf_data.node;
		}
		// 关系
		if(uxdf_data && uxdf_data.event){
			let eventNames = Object.keys(uxdf_data.event);
			for(let curName of eventNames){
				uxdf_events = uxdf_events.concat(uxdf_data.event[curName]);
			}
		}
		// 补充属性
		let mappingSdNames = Object.keys(MAPPING);
		for(let curNode of uxdf_nodes){
			let curSd = curNode['__sd'];
			curNode.id = curNode['__id'];
			if(mappingSdNames.indexOf(curSd) > -1){
				Object.assign(curNode,MAPPING[curSd]);
			}else{
				Object.assign(curNode,MAPPING['NODE']);
				curNode.fill = this.generateNodeFill(curSd);
			}
			curNode.group = this.generateNodeGroup(curSd);
			let titleStr = "",
				titleProps = TITLE_PROPS[curSd];
			if(titleProps){
				for(let curProp of titleProps){
					titleStr += curNode[curProp];
				}
				if(titleStr){
					curNode.title = titleStr;
				}
			}
			result.nodes.push(curNode);
		}
		// 补充关系
		for(let curEvent of uxdf_events){
			let curSd = curEvent['__sd'],
				curSource,
				curTarget;
			for(let curNode of result.nodes){
				if(curNode['__id'] == curEvent['__left']){
					curSource = curNode;
				}else if(curNode['__id'] == curEvent['__right']){
					curTarget = curNode;
				}
				if(curSource && curTarget){
					break;
				}
			}
			curEvent.id = curEvent['__id'];
			if(mappingSdNames.indexOf(curSd) > -1){
				Object.assign(curEvent,MAPPING[curSd],{"source":curSource,"target":curTarget});
			}else{
				Object.assign(curEvent,MAPPING['EVENT'],{"source":curSource,"target":curTarget});
				// fill 颜色
			}
			curEvent['arrow'] = "positive";
			if(!curEvent[EVENT_ARROW.KEY]){
				curEvent[EVENT_ARROW.KEY] = EVENT_ARROW.POSITIVE;
			}
			let titleStr = "",
				titleProps = TITLE_PROPS[curSd];
			if(titleProps){
				for(let curProp of titleProps){
					titleStr += curEvent[curProp];
				}
				if(titleStr){
					curEvent.title = titleStr;
				}
			}
			result.links.push(curEvent);
		}
		return result;
	}
};
module.exports = DataUtil;
